{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import gym\n",
    "import numpy as np\n",
    "import torch\n",
    "import os\n",
    "from torch.utils.tensorboard import SummaryWriter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class ActionNet(torch.nn.Module):\n",
    "    def __init__(self, input_dims, output_dims):\n",
    "        super().__init__()\n",
    "        self.fc1 = torch.nn.Linear(input_dims, 30)\n",
    "        self.fc2 = torch.nn.Linear(30, output_dims)\n",
    "        self.tanh = torch.nn.Tanh()\n",
    "        self.softmax = torch.nn.Softmax()\n",
    "        self.initialize_weights()\n",
    "\n",
    "    def initialize_weights(self):\n",
    "        for m in self.modules():\n",
    "            if isinstance(m, torch.nn.Linear):\n",
    "                torch.nn.init.normal_(m.weight, std=0.3)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.tanh(self.fc1(x))\n",
    "        x = self.softmax(self.fc2(x))\n",
    "        return x"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "343f4162277aa3df"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class PolicyGradient:\n",
    "    def __init__(self,\n",
    "                 n_state=4,\n",
    "                 n_actions=2,\n",
    "                 learning_rate=0.01,\n",
    "                 reward_decay=0.95):\n",
    "        self.n_actions = n_actions\n",
    "        self.n_features = n_state\n",
    "        self.lr = learning_rate\n",
    "        self.gamma = reward_decay\n",
    "\n",
    "        self.s_lst = []\n",
    "        self.a_lst = []\n",
    "        self.r_lst = []\n",
    "\n",
    "        self.net = ActionNet(n_state, n_actions)\n",
    "        # self.loss = torch.nn.CrossEntropyLoss()\n",
    "        self.optimizer = torch.optim.Adam(self.net.parameters(), lr=learning_rate)\n",
    "\n",
    "\n",
    "    def choose_action(self, states):\n",
    "        self.net.eval()\n",
    "        actions = self.net(torch.Tensor(states[np.newaxis, :]))\n",
    "        action = np.random.choice(range(actions.shape[1]), p=actions.view(-1).detach().numpy())\n",
    "        return action\n",
    "    \n",
    "    \n",
    "    def store_transition(self, s, a, r):\n",
    "        self.a_lst.append(a)\n",
    "        self.r_lst.append(r)\n",
    "        self.s_lst.append(s)\n",
    "    \n",
    "    \n",
    "    def to_onehot(self, a_lst, action_n):\n",
    "        onehot = torch.zeros(size=(len(a_lst), 2)).scatter(1, torch.LongTensor(a_lst).view(-1, 1), 1)\n",
    "        return onehot\n",
    "    \n",
    "    \n",
    "    def learn(self):\n",
    "        self.net.train()\n",
    "        discounted = np.zeros_like(self.r_lst)\n",
    "        running_add = 0\n",
    "        for i in range(len(self.r_lst) - 1, -1, -1):\n",
    "            running_add = running_add * self.gamma + self.r_lst[i]\n",
    "            discounted[i] = running_add\n",
    "        discounted = discounted - np.mean(discounted)\n",
    "        discounted = discounted / np.std(discounted)\n",
    "        output = self.net(torch.tensor(self.s_lst))\n",
    "        onehot = self.to_onehot(self.a_lst, self.n_actions)\n",
    "        # PG算法使用交叉熵损失函数\n",
    "        neg = torch.sum(-torch.log(output) * onehot, 1)\n",
    "        loss = neg * torch.Tensor(discounted)\n",
    "        loss = loss.mean()\n",
    "        self.optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        self.optimizer.step()\n",
    "        # 重置列表\n",
    "        self.s_lst = []\n",
    "        self.a_lst = []\n",
    "        self.r_lst = []\n",
    "        return discounted, loss"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "85f64d98470d7f61"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "log_dir = './runs'\n",
    "if os.path.exists(log_dir):\n",
    "    try:\n",
    "        shutil.rmtree(log_dir)\n",
    "        print(f'文件夹 {log_dir} 已成功删除。')\n",
    "    except OSError as error:\n",
    "        print(f'删除文件夹 {log_dir} 失败: {error}')\n",
    "else:\n",
    "    os.makedirs(log_dir)\n",
    "    print(f'文件夹 {log_dir} 不存在，已创建文件夹 {log_dir}。')"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8d25abee5899db56",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "summary_writer = SummaryWriter(log_dir=log_dir)\n",
    "env = gym.make(\"CartPole-v1\")\n",
    "gamma = 0.9\n",
    "num_episodes = 500\n",
    "pg = PolicyGradient(\n",
    "    n_state=env.observation_space.shape[0],\n",
    "    n_actions=env.action_space.n,\n",
    "    learning_rate=0.01,\n",
    "    reward_decay=0.95\n",
    ")\n",
    "net = ActionNet(4, 2)\n",
    "for i in range(num_episodes):\n",
    "    # start_time = time.time()\n",
    "    s, _ = env.reset()\n",
    "    # total_reward = 0\n",
    "    total_loss = 0\n",
    "    step = 0\n",
    "    reward_dict = {'running_reward': None}\n",
    "    while True:\n",
    "        a = pg.choose_action(s)\n",
    "        ns, r, done, _, _ = env.step(a)\n",
    "        step += 1\n",
    "        pg.store_transition(s, a, r)\n",
    "\n",
    "        if done:\n",
    "            r_lst_sum = sum(pg.r_lst)\n",
    "\n",
    "            # if 'running_reward' not in globals():\n",
    "            #     running_reward = r_lst_sum\n",
    "            # else:\n",
    "            #     running_reward = running_reward * 0.9 + r_lst_sum * 0.1\n",
    "            if reward_dict['running_reward'] is None:\n",
    "                reward_dict['running_reward'] = r_lst_sum\n",
    "            else:\n",
    "                reward_dict['running_reward'] = reward_dict['running_reward'] * 0.9 + r_lst_sum * 0.1\n",
    "            \n",
    "            print(\"episode:\", i, \"  reward:\", int(reward_dict['running_reward']))\n",
    "            vt, loss = pg.learn()\n",
    "            summary_writer.add_scalar('reward', reward_dict['running_reward'], i)\n",
    "            break\n",
    "        s = ns\n",
    "        "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "eea716283460c115"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "env = gym.make(\"CartPole-v1\", render_mode='human')\n",
    "s, _ = env.reset()\n",
    "step = 0\n",
    "while True:\n",
    "    s = torch.tensor(s)\n",
    "    action = pg.choose_action(s)\n",
    "    s, r, done, _, _ = env.step(action)\n",
    "    step += 1\n",
    "    if done:\n",
    "        break\n",
    "print('step:{}'.format(step))\n",
    "env.close()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5af05addcf744d3b"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "env.close()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "52967f6d08a58951"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
